package com.baiyun.controller.admin;

import com.baiyun.Result.Result;
import com.baiyun.entity.User;
import com.baiyun.service.LoginService;
import com.baiyun.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/login")
@RequiredArgsConstructor
@Api(tags = "登录管理")
public class LoginController {
    private final LoginService loginService;
    private final JwtUtils jwtUtils;

    @ApiOperation("登录")
    @PostMapping
    public Result login(@RequestBody Map<String, String> body) {
        String username = body.get("username");
        String password = body.get("password");
        log.info("login登录,username:{},password:{}", username, password);
        User user = loginService.login(username, password);
        if (user != null){
            Map<String, Object> userInfo = new HashMap<>();
            userInfo.put("uid", user.getUid());
            userInfo.put("username", user.getUsername());
            userInfo.put("type", user.getType());
            return Result.success(jwtUtils.createJWT(userInfo));
        }
        return Result.error("登录失败");
    }
}
